package com.tarena.lbs.common.marketing.converter;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.marketing.enums.ActivityStatusEnum;
import com.tarena.lbs.common.marketing.enums.ActivityTypeEnum;
import com.tarena.lbs.common.marketing.enums.ChannelTypeEnum;
import com.tarena.lbs.common.marketing.enums.MarketingResultEnum;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.security.utils.LbsSecurityContenxt;
import com.tarena.lbs.pojo.marketing.bo.MarketingActivityBO;
import com.tarena.lbs.pojo.marketing.dos.MarketingActivityDO;
import com.tarena.lbs.pojo.marketing.param.MarketingActivityParam;
import com.tarena.lbs.pojo.marketing.query.MarketingActivityQuery;
import com.tarena.lbs.pojo.marketing.vo.MarketingActivityVO;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;

@Configuration
public class MarketingActivityConverter {

    public PageResult<MarketingActivityVO> assemblePageVOS(BasePageList<MarketingActivityBO> bos,
        MarketingActivityQuery query) {
        if (bos == null || bos.getTotal() == 0) {
            return null;
        }
        PageResult<MarketingActivityVO> result = new PageResult<>();
        List<MarketingActivityVO> vos = new ArrayList<>();
        for (MarketingActivityBO temp : bos.getList()) {
            MarketingActivityVO vo = new MarketingActivityVO();
            BeanUtils.copyProperties(temp,vo);
            vos.add(vo);
        }
        result.setTotal(bos.getTotal());
        result.setObjects(vos);
        result.setPageNo(query.getPageNo());
        result.setPageSize(query.getPageSize());
        return result;
    }

    public MarketingActivityVO bo2vo(MarketingActivityBO bo) {
        if (bo == null) {
            return null;
        }
        MarketingActivityVO vo = new MarketingActivityVO();
        BeanUtils.copyProperties(bo,vo);
        return vo;
    }

    public MarketingActivityBO do2bo(MarketingActivityDO tempDo) {
        if (tempDo == null) {
            return null;
        }
        MarketingActivityBO bo = new MarketingActivityBO();
        BeanUtils.copyProperties(tempDo,bo);
        return bo;
    }

    public List<MarketingActivityBO> dos2bos(List<MarketingActivityDO> dos) {
        if (CollectionUtils.isEmpty(dos)) {
            return null;
        }
        List<MarketingActivityBO> bos = new ArrayList<>();
        for (MarketingActivityDO temp : dos) {
            MarketingActivityBO bo = new MarketingActivityBO();
            BeanUtils.copyProperties(temp,bo);
            bos.add(bo);
        }
        return bos;
    }

    public MarketingActivityDO param2DO(MarketingActivityParam param) throws BusinessException {
        MarketingActivityDO tempDO = new MarketingActivityDO();
        BeanUtils.copyProperties(param, tempDO);
        BeanUtils.copyProperties(param,tempDO);
        tempDO.setCreateAt(new Date());
        tempDO.setRewardType(1);
        tempDO.setChannelType(ChannelTypeEnum.APP.getCode());
        tempDO.setActivityType(ActivityTypeEnum.GIVE.getCode());
        tempDO.setEnableStatus(ActivityStatusEnum.DISABLE.getCode());
        tempDO.setStatus(ActivityStatusEnum.DISABLE.getCode());
        try {
            tempDO.setStartDate(DateUtils.parseDate(param.getStartDate(),"yyyy-MM-dd HH:mm:ss"));
            tempDO.setEndDate(DateUtils.parseDate(param.getEndDate(),"yyyy-MM-dd HH:mm:ss"));
        } catch (Exception e) {
            throw new BusinessException(MarketingResultEnum.DATE_ERROR);
        }
        if (tempDO.getStartDate().before(new Date()) && tempDO.getEndDate().after(new Date())) {
            tempDO.setStatus(ActivityStatusEnum.ENABLE.getCode());
        }
        UserPrinciple loginUser = LbsSecurityContenxt.getLoginToken();
        if (loginUser != null) {
            tempDO.setBusinessId(loginUser.getId().intValue());
        }
        return tempDO;
    }
}
